<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${package}.db.mapper.${ClassName}Mapper">

    <resultMap id="BaseResultMap" type="${package}.db.model.${ClassName}">
        <#list columnList as column>
        <result column="${column.columnName}" property="${column.attrName}"/>  <!-- ${column.columnComment} -->
        </#list>
    </resultMap>

    <sql id="BaseColumn">
        <#list columnList as column>${tableAlias}.${column.columnName}<#if column_has_next>,</#if></#list>
    </sql>

    <!-- 根据条件查询 -->
    <select id="selectByWhere"
            parameterType="${package}.dto.${classname}.req.${ClassName}ListDto"
            resultMap="BaseResultMap">
        select
        <include refid="BaseColumn"/>
        from ${tableName} as ${tableAlias}
        <where>
            ${tableAlias}.deleted = 1
            <if test="customerId != null and customerId > 0">
                and ${tableAlias}.customer_id = <#noparse>#{customerId}</#noparse>
            </if>
            <if test="orgId != null and orgId > 0">
                and find_in_set(<#noparse>#{orgId}</#noparse>,${tableAlias}.full_index)
            </if>
            <#list columnList as column>
            <#if column.attrName == "enterpriseId">
            <if test="enterpriseId != null">
                and find_in_set(<#noparse>#{</#noparse>enterpriseId<#noparse>}</#noparse>, ${tableAlias}.full_index_all)
            </if>
            </#if>
            <#if column.query && column.attrName != "enterpriseId">
            <#if column.columnType == "bigint">
            <if test="${column.attrName} != null">
                and ${tableAlias}.${column.columnName} ${column.queryType} <#noparse>#{</#noparse>${column.attrName}<#noparse>}</#noparse>
            </if>
            <#elseif column.columnType == "int">
            <if test="${column.attrName} != null">
                and ${tableAlias}.${column.columnName} ${column.queryType} <#noparse>#{</#noparse>${column.attrName}<#noparse>}</#noparse>
            </if>
            <#elseif column.columnType == "varchar" && column.queryType == 'like'>
            <if test="${column.attrName} != null and ${column.attrName} != ''">
                and ${tableAlias}.${column.columnName} ${column.queryType} CONCAT('%',<#noparse>#{</#noparse>${column.attrName}<#noparse>}</#noparse>,'%')
            </if>
            <#elseif column.columnType == "varchar" && column.queryType == '='>
            <if test="${column.attrName} != null and ${column.attrName} != ''">
                and ${tableAlias}.${column.columnName} ${column.queryType} <#noparse>#{</#noparse>${column.attrName}<#noparse>}</#noparse>
            </if>
            <#elseif column.columnType == "decimal">
            <if test="${column.attrName} != null">
                and ${tableAlias}.${column.columnName} ${column.queryType} <#noparse>#{</#noparse>${column.attrName}<#noparse>}</#noparse>
            </if>
            <#elseif column.columnType == "date" || column.columnType == "datetime">
            <if test="${column.attrName}StartStr != null and ${column.attrName}StartStr != ''">
                and ${tableAlias}.${column.columnName} <![CDATA[>=]]> <#noparse>'${</#noparse>${column.attrName}StartStr<#noparse>}'</#noparse>
            </if>
            <if test="${column.attrName}EndStr != null and ${column.attrName}EndStr != ''">
                and ${tableAlias}.${column.columnName} <![CDATA[<=]]> <#noparse>'${</#noparse>${column.attrName}EndStr<#noparse>}'</#noparse>
            </if>
            </#if>
            </#if>
            </#list>
        </where>
    </select>

</mapper>